﻿/* you must remove all function before you can delete assembly */

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[CSV]') AND type = N'AF')
	DROP AGGREGATE DbExt.CSV
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileWriteAllLines]') AND type = N'AF')
	DROP AGGREGATE DbExt.FileWriteAllLines
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Skewness]') AND type = N'AF')
	DROP AGGREGATE DbExt.Skewness
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Kurtosis]') AND type = N'AF')
	DROP AGGREGATE DbExt.Kurtosis
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Median]') AND type = N'AF')
	DROP AGGREGATE DbExt.Median
GO


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Dir]'))
	DROP FUNCTION DbExt.Dir 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileCopy]'))
	DROP FUNCTION DbExt.FileCopy 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileDelete]'))
	DROP FUNCTION DbExt.FileDelete
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileExists]'))
	DROP FUNCTION DbExt.FileExists 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileMove]'))
	DROP FUNCTION DbExt.FileMove 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileRename]'))
	DROP FUNCTION DbExt.FileRename 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileReadAllLines]'))
	DROP FUNCTION DbExt.FileReadAllLines 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileReadAllText]')) 
	DROP FUNCTION DbExt.FileReadAllText 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FileWriteAllText]'))
	DROP FUNCTION DbExt.FileWriteAllText 
GO


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[ShareConnect]')) 
	DROP FUNCTION DbExt.ShareConnect 
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[ShareDisconnect]'))
	DROP FUNCTION DbExt.ShareDisconnect 
GO


IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[DirDelete]'))
	DROP FUNCTION DbExt.DirDelete
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[DirCreate]'))
	DROP FUNCTION DbExt.DirCreate
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[DirExists]'))
	DROP FUNCTION DbExt.DirExists
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[DirMove]'))
	DROP FUNCTION DbExt.DirMove
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[DirRename]'))
	DROP FUNCTION DbExt.DirRename
GO

/*
	This section is needed due to compatibility issues
	The following functions were renamed in a early stage
*/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StringSplit]'))
	DROP FUNCTION DbExt.StringSplit
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StringLPad]')) 
	DROP FUNCTION DbExt.StringLPad
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StringRPad]'))
	DROP FUNCTION DbExt.StringRPad
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StringSubStr]'))
	DROP FUNCTION DbExt.StringSubStr
GO




IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Split]'))
	DROP FUNCTION DbExt.Split
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[LPad]'))
	DROP FUNCTION DbExt.LPad
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[RPad]')) 
	DROP FUNCTION DbExt.RPad
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[SubStr]'))
	DROP FUNCTION DbExt.SubStr
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Remove]'))
	DROP FUNCTION DbExt.Remove
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[TrimStart]'))
	DROP FUNCTION DbExt.TrimStart
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[TrimEnd]'))
	DROP FUNCTION DbExt.TrimEnd
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StartsWith]'))
	DROP FUNCTION DbExt.StartsWith
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[EndsWith]'))
	DROP FUNCTION DbExt.EndsWith
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Contains]'))
	DROP FUNCTION DbExt.[Contains]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Translate]'))
	DROP FUNCTION DbExt.[Translate]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[StringFormat]'))
	DROP FUNCTION DbExt.[StringFormat]
GO
------------------------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[FtpUpload]'))
	DROP FUNCTION DbExt.FtpUpload
GO
------------------------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[RailDigitSum]'))
	DROP FUNCTION DbExt.RailDigitSum
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[RailCheckDigit]'))
	DROP FUNCTION DbExt.RailCheckDigit
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[ExportToCSV]'))
	DROP FUNCTION DbExt.ExportToCSV
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Zeitraeume]'))
	DROP FUNCTION DbExt.Zeitraeume
GO
------------------------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[QueryToCsv]'))
	DROP PROCEDURE DbExt.QueryToCsv;
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[QueryToHtml]'))
	DROP PROCEDURE DbExt.QueryToHtml;
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[QueryToExcelXml]'))
	DROP PROCEDURE DbExt.QueryToExcelXml;
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[QueryToXml]'))
	DROP PROCEDURE DbExt.QueryToXml;
GO
------------------------------------------------------------------------------------------------
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[DbExt].[Version]'))
	DROP FUNCTION DbExt.[Version]
GO

/* deletes the old assembly */
IF  EXISTS (SELECT * FROM sys.assemblies asms WHERE asms.name = N'sqlserverextensions' and is_user_defined = 1)
DROP ASSEMBLY [sqlserverextensions]
GO
